<template>
  <el-dialog
    :visible="visible"
    :title="title"
    class="add-sub-project-dialog"
    align-center
    @opened="handleDialogOpen"
    @close="closeDialog"
    width="40%"
  >
    <el-form
      :model="form"
      :disabled="disabled"
      :rules="formRules"
      ref="formRef"
      label-width="160px"
    >
      <el-form-item prop="district" label="区县">
        <el-cascader
          v-model="areaInfo"
          :options="cityList"
          :props="casxaderProps"
          placeholder="请选择区县"
          class="area-cascader"
          :show-all-levels="false"
          @change="handleChangeArea"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="子项目名称" prop="subPeName">
        <el-input
          v-model="form.subPeName"
          placeholder="请输入子项目名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="所属项目名称" prop="peId">
        <!--        <el-input-->
        <!--          v-model="form.peId"-->
        <!--          placeholder="请输入所属项目名称"-->
        <!--        ></el-input>-->
        <el-select
          v-model="form.peId"
          placeholder="请选择所属项目名称"
          filterable
        >
          <el-option
            v-for="item in projectList"
            :key="item.id"
            :label="item.peName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否入统" prop="isRt">
        <el-radio-group v-model="form.isRt">
          <el-radio label="Y">是</el-radio>
          <el-radio label="N">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="入统行业代码" prop="rtCode" v-if="form.isRt === 'Y'">
        <el-input
          v-model="form.rtCode"
          placeholder="请输入入统行业代码"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="onSubmit"> 确定 </el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import {
  addSubProjectInventory,
  updateSubProjectInventory,
} from '@/api/project-update/sub-project-inventory'
import { Message } from 'element-ui'
import { getProjectInventoryList } from '@/api/project-update/project-inventory'

const casxaderProps = {
  value: 'areaCode',
  label: 'areaName',
  // checkStrictly: true,
  children: 'children',
}
const areaInfo = ref([])

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: '新增子项目',
  },
  item: {
    type: Object,
    default: () => ({}),
  },
  cityList: {
    type: Array,
    default: () => [],
  },
})

const disabled = computed(() => props.title === '子项目详情')

const emit = defineEmits(['closeDialog'])

const closeDialog = (flag) => {
  const obj = {
    district: '',
    subPeName: '',
    peId: '',
    isRt: 'Y',
    rtCode: '',
    id: '',
  }
  Object.assign(form, obj)
  if (flag === 'getList') {
    emit('closeDialog', 'getList')
  } else {
    emit('closeDialog')
  }
}

const projectList = ref([])

const form = reactive({
  district: '',
  subPeName: '',
  peId: '',
  isRt: 'Y',
  rtCode: '',
  id: '',
})

const handleChangeArea = (val) => {
  form.district = val[val.length - 1]
}

const formRef = ref()

const formRules = reactive({
  district: [{ required: true, message: '请输入区县', trigger: 'blur' }],
  subPeName: [{ required: true, message: '请输入子项目名称', trigger: 'blur' }],
  peId: [{ required: true, message: '请输入所属项目名称', trigger: 'blur' }],
  isRt: [{ required: true, message: '请选择是否入统', trigger: 'blur' }],
  rtCode: [{ required: true, message: '请输入入统行业代码', trigger: 'blur' }],
})

const handleDialogOpen = () => {
  // console.log(props.item)
  if (props.item) {
    Object.assign(form, props.item)
  }
  if (form.district) {
    const f = props.cityList.find((item) => {
      return item.children.find((i) => {
        if (i.areaName === form.district) {
          return i
        }
      })
    })
    if (f) {
      const cF = f.children.find((item) => {
        return item.areaName === form.district
      })
      areaInfo.value = [f.pkid, cF.pkid]
      form.district = cF.pkid
    }
  }
}

const onSubmit = () => {
  if (props.title === '子项目详情') {
    closeDialog()
    return
  }
  formRef.value?.validate((valid) => {
    if (valid) {
      if (!form.id) {
        addSubProjectInventory(form).then((res) => {
          if (res.success) {
            Message.success(res.data)
            closeDialog('getList')
          } else {
            Message.error(res.message)
          }
        })
      } else {
        updateSubProjectInventory(form).then((res) => {
          if (res.success) {
            Message.success(res.data)
            closeDialog('getList')
          } else {
            Message.error(res.message)
          }
        })
      }
    }
  })
}

const getProjectList = () => {
  getProjectInventoryList({
    pageSize: 999,
  }).then((res) => {
    projectList.value = res.data.list
  })
}

onMounted(() => {
  getProjectList()
})
</script>

<style lang="scss">
.add-sub-project-dialog {
  //width: 762px;
  //height: 704px;
  //background: #ffffff;
  border-radius: 4px;

  .el-dialog__header {
    border-bottom: 1px solid #e5e5e5;

    .el-dialog__title {
      width: 64px;
      height: 16px;
      font-size: 16px;
      font-weight: bold;
      color: #27475f;
    }
  }

  .el-form {
    //margin-bottom: 336px;
    .el-form-item {
      min-height: 42px;
      margin-bottom: 21px;
      align-items: center;
    }

    .el-cascader {
      width: 100%;
    }

    .el-select {
      width: 100%;
    }
  }

  .el-dialog__footer {
    padding-top: 30px;
    border-top: 1px solid #e5e5e5;
  }

  .dialog-footer {
    width: 100%;
    height: 100%;
    text-align: center;
  }
}
</style>
